package main

import (
	"encoding/xml"
	"fmt"
	"os"
)

type Languages struct {
	XMLName xml.Name   `xml:"languages"`
	Version string     `xml:"version,attr"`
	Lang    []Language `xml:"language"`
}

type Language struct {
	Name string `xml:"name"`
	Site string `xml:"site"`
}

func main() {
	obj := Languages{Version: "2"}
	obj.Lang = append(obj.Lang, Language{"Java", "https://www.java.com"})
	obj.Lang = append(obj.Lang, Language{"Go", "https://golang.org"})
	data, err := xml.MarshalIndent(obj, " ", " ") //将结构体对象写入字节数组
	if err != nil {
		fmt.Printf("error %v", err)
		return
	}

	file, _ := os.Create("cp6_file/xml/languages.xml") //写入xml文件
	defer file.Close()
	file.Write([]byte(xml.Header)) //先写xml头部（<?xml version="1.0" encoding="UTF-8"?>）
	file.Write(data)               //写入数据
}
